import { createI18n } from 'vue-i18n'

import elementZhLocale from 'element-plus/lib/locale/lang/zh-cn'
import elementEnLocale from 'element-plus/lib/locale/lang/en'
import zhLocale from './zh'
import enLocale from './en'
const messages = {
  zh: {
    el: elementZhLocale.el,
    ...zhLocale
  },
  en: {
    el: elementEnLocale.el,
    ...enLocale
  }
}

export function getLanguage() {
  const chooseLanguage = localStorage.getItem('language')

  if (chooseLanguage) return chooseLanguage

  const language = (navigator.language || navigator.browserLanguage).toLowerCase()
  const locales = Object.keys(messages)
  for (const locale of locales) {
    if (language.indexOf(locale) > -1) {
      return locale
    }
  }
  return 'zh-cn'
}

const i18n = createI18n({
  legacy: false, // 如果要在 Compostion API 中使用  必须要设为false
  // 根据浏览器以及缓存内容来确认语言类型
  locale: getLanguage(),
  messages
})

export default i18n
